﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
using System.Diagnostics;
using System.Text.RegularExpressions;


namespace Utils
{

		/// <summary>
		/// Used for parsing the key="values values" part of the description
		/// </summary>
		public class DescParser
		{
				public ListDictionary  Params = new ListDictionary();			//comm -- type of Hash with unique keys ...
				//public HybridDictionary Params = new HybridDictionary ( 3 , true ) ;//comm -- type of Hash with unique keys ...


				/// <summary>
				/// Fills a StringDictionary with key value pars (removes the double quotes from the strings
				/// </summary>
				/// <param name="parsable"> the string to parse by reference</param>

				public DescParser ( object parsableObj )
				{
						string parsable = System.Convert.ToString ( parsableObj );

						if (parsable == null || parsable.Equals ( String.Empty ))
						{
								throw new Exception ( " the FillHash was passed with null or empty parsable. The parsable is " + parsable );
						}
						Regex Remover = new Regex ( @"^['""]?(.*?)['""]?$" ,
						RegexOptions.IgnoreCase | RegexOptions.Compiled );


						string[] tokens = parsable.Split ( new Char [] { ',' } );
						foreach (string token in tokens)
						{
								string[] pairs = token.Split ( new Char [] { '=' } );
								pairs [ 1 ] = Remover.Replace ( pairs [ 1 ] , "$1" );
								pairs [ 0 ] = Remover.Replace ( pairs [ 0 ] , "$1" );
								//debugOk Utils.Debugger.WriteLine ( "My pairs [0] is " + pairs [ 0 ] );
								//debugOk Utils.Debugger.WriteLine ( "My pairs [1] is " + pairs [ 1 ] );
								Params.Add ( (object) pairs [ 0 ] , (object) pairs [ 1 ] );
						} //eof foreach


				} //eof constructor DescParser

				public string giveMe ( string key )
				{
						object keyObj = (object) key;
						return System.Convert.ToString ( Params [ keyObj ]  );
				} //public string this[string Param]
		} //eof class DescParser

/**TESTING
		class Tester
		{
				static void Main ( string [] args )
				{

						string parsable = "key0=\"one value0\",key1=\"one_value1\",key2=value2,key3=value3";
						//"title=\"some title\",visible=\"1\",fs=\"some fieldset\""


						Utils.DescParser dp = new Utils.DescParser ( parsable );
						Console.WriteLine ( "my value is value = OK " );
						Console.WriteLine ( "my value0 is " + dp.giveMe ( "key0" ) );
						Console.WriteLine ( "my value1 is " + dp.giveMe ( "key1" ) );
						Console.WriteLine ( "my value2 is " + dp.giveMe ( "key2" ) );
						Console.WriteLine ( "my value3 is " + dp.giveMe ( "key3" ) );
						System.Threading.Thread.Sleep ( 5000 );        //sleep for 5 seconds

				} //eof main} //eof class TEster 
		} //eof class
 */

} //eof namespace Utils





